package com.nativetalk.service.three.live.likes;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TdNtLikes;
import com.nativetalk.bean.live.likes.TdNtLikesList;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.dao.TdNtLikesDao;
import com.nativetalk.dao.TdNtLiveCourseHisDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2016/9/27.
 */
@Service
public class LikesServiceImpl implements LikesService {
    private static Logger logger = Logger.getLogger(LikesServiceImpl.class);
    @Resource
    private TdNtLikesDao tdNtLikesDao;
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Override
    @Transactional
    public synchronized RetInfo liveLikes(TdNtLikes tdNtLikes) {
        String logInfo = this.getClass().getName() + ":liveLikes:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtLikes likes = tdNtLikesDao.findTdNtLikesById(tdNtLikes);
            int likes_size = tdNtLiveCourseHisDao.findLivesLikes(tdNtLikes.getLive_cou_his_id());
            if (likes != null) {
                if (likes.getLikes_state() == 0) {
                    likes.setLikes_state(1);
                    likes_size = likes_size - 1;
                } else {
                    likes.setLikes_state(0);
                    likes_size = likes_size + 1;
                }
                tdNtLikesDao.updateTdNtLikes(likes);
            } else {
                tdNtLikes.setLikes_month(CommonUtil.getMonth(new Date()));
                tdNtLikes.setLikes_state(0);
                tdNtLikesDao.insertTdNtLikes(tdNtLikes);
                likes_size = likes_size + 1;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("live_cou_his_id", tdNtLikes.getLive_cou_his_id());
            map.put("likes_size", likes_size);
            tdNtLiveCourseHisDao.updateLivesLikes(map);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    @Override
    public RetInfo list(Page page) {
        String logInfo = this.getClass().getName() + ":list:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLikesDao.findTdNtLikesByLiveHisIdSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtLikesList> tdNtLikesLists = tdNtLikesDao.findTdNtLikesByLiveHisIdPage(page);
                if ( tdNtLikesLists != null && tdNtLikesLists.size() > 0) {
                    for (int i = 0 ; i < tdNtLikesLists.size() ; i ++) {
                        TdNtLikesList tdNtLikesList = tdNtLikesLists.get(i);
                        String name = CommonUtil.substring(tdNtLikesList.getMemb_name(),0,1) + "****" +
                                CommonUtil.substring(tdNtLikesList.getMemb_name(),tdNtLikesList.getMemb_name().length() - 1,tdNtLikesList.getMemb_name().length());
                        tdNtLikesList.setMemb_name(name);
                    }
                }
                result.put("likes", tdNtLikesLists);
                result.put("totalPage", totalPage);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播赞成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("暂无直播赞");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
